<?php
namespace app\admin\model;
use think\Model;
use think\Db;

/**
 * 新渠道短信配置
 * @package app\admin\model
 */
class BusChannelSmsConfig extends Model
{
    public static function getList($request)
    {
        $where = [];
        // if(!empty($request['ctn'])) {
        //     $where['sc.sms_ctn']  = ['like', '%' . $request['ctn'] . '%'];
        // }
        if(!empty($request['cid'])) {
            $where[]  = ['c.channel_id','eq', $request['cid']];
        }
        if(isset($request['onoff']) && $request['onoff'] != '') {
            $where[]  = ['csc.onoff','eq', $request['onoff']];
        }

        return self::alias('csc')->field('csc.*,ss.sms_sign,sc.sms_ctn,c.company_name')
                                ->join('bus_sms_sign ss', 'csc.sign_id = ss.id')
                                ->join('bus_sms_ctn sc', 'csc.ctn_id = sc.id')
                                ->join('bus_channel c', 'csc.cid = c.channel_id')
                                ->where($where)
                                ->paginate($request['limit'], false,['query' => $request])
                                ->each(function($item, $key) {
                                    switch ($item->onoff) {
                                        case '1':
                                            $item->onoff = '<span class="layui-badge layui-bg-green">使用中</span>';
                                            break;
                                        case '0':
                                            $item->onoff = '<span class="layui-badge layui-bg-orange">已禁用</span>';
                                            break;
                                        default:
                                            # code...
                                            break;
                                    }
                                    $item->sign_ctn = $item->sms_sign. ' ' .$item->sms_ctn;

                                });
    }

    public static function add($data)
    {
        return self::create($data);
    }
}